$(function() {
	//提交广告数据
	$('#submitAD').click(function() {
		iAmPutAD(this);
	});
	$('#adDesc').keyup(function () {
		let adDesc = V('adDesc');
		let adDescLength = adDesc.length, wordNum = 0;
		if (adDescLength > 200){
			adDesc.val(adDesc.substring(0,200));
		} else {
			wordNum = 200 - adDescLength;
		}
		$('#wordNum').text(wordNum);
	});
});

/**
 * 我要提交广告
 */
function iAmPutAD(btnPutAD) {
	if (!checkServiceForm()) {
		return;
	}
	setButtonStatus(btnPutAD,true,'提交中...');
	$.ajax({
		type: 'post',
		url : '/ventIndex/savePutAD',
		dataType: 'json',
		timeout: 120000,
		data: {
            'phone': V('adContact'),
            'budget': V('adBudget'),
            'name': V('adName'),
            'company': V('adCompany'),
			'adDesc': V('adDesc'),
			'adStartDate': V('adStartDate'),
			'adEndDate': V('adEndDate')
		},
		success: function (adData) {
			setButtonStatus(btnPutAD,false,'提交广告');
			if(adData.row < 1){
				setErrorMsg('提交失败，请稍候再试。');
				return;
			}
			setErrorMsg('您的广告投放申请已提交，我们会尽快联系您。');
			$('.fields input').val('');
			$('#adDesc').text('');
		},
		error: function () {
			setErrorMsg("下单失败，请稍候再试");
			setButtonStatus(btnPutAD,false,'提交广告');
		}
	});
}

function setErrorMsg(e) {
	$('#errorMsg').text(e);
}
/**
 * 检查表单必填项
 */
function checkServiceForm() {
	let adContact = V('adContact');
    if (isNull(adContact) || /.*[\u4e00-\u9fa5]+.*$/.test(adContact)) {
        setErrorMsg('请输入联系电话或邮箱');
        return false;
    }
    if (adContact.indexOf('@') > 0){
    	if (!checkEmail(adContact)){
			setErrorMsg('邮箱不正确');
    		return false;
		}
	} else {
		if (!phoneNumberIsRight(adContact)){
			setErrorMsg('手机号不正确');
			return false;
		}
	}
    if (isNull(V('adBudget')) || V('adBudget') <= 0) {
        setErrorMsg('请输入正确的预算金额');
        return false;
    }
	setErrorMsg('');
	return true;
}